package com.itheima.day13.thread.test11;

import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread {
    private static int tickets = 100;
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while(true){
            lock.lock(); // 获得锁
            if(tickets <= 0){
                break;
            }
            try {
                Thread.sleep(10);
                tickets--;
                System.out.println(Thread.currentThread().getName() + " 正在买票，还剩 " + tickets + " 张票。");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock(); // 释放锁
            }
        }
    }
}
